window.onload = function() {
    document.getElementById("message").onclick = function () {
        var a = document.querySelectorAll('dl>dd')
        for(i=0;i<4;i++){
            a[i].classList.toggle("hide")
        }
    }
    document.getElementById("superSail").onclick = function () {
        var a = document.querySelectorAll('dl>dd')
        for(i=4;i<6;i++){
            a[i].classList.toggle("hide")
        }
    }
    document.getElementById("order").onclick = function () {
        var a = document.querySelectorAll('dl>dd')
        for(i=6;i<a.length;i++){
            a[i].classList.toggle("hide")
        }
    } 

//----------------------------------------------------------------------------------------------------//
//----------------------------------------------------------------------------------------------------//
//----------------------------------------------------------------------------------------------------//

    function queryRefuseExpress(pageNo){
        let pageSize =parseInt(document.getElementById("pageSize").value)
        axios.get('/home/expressmessage/express/userSureOrNoSent',{
            params:{
                pageSize:pageSize,
                pageNo:pageNo,
            }
        }).then(function(data){
            console.log(data)
            console.log(10086)
            var list = data.data.data.data
            if(list.length == 0){
                document.getElementsByClassName("mainDiv").innerHTML = `
                <div class="myMessage" id="myMessage">
                </div>
                `
                document.getElementById("myMessage").innerHTML = ``
                document.getElementById("myMessage").innerHTML += `
                <div class="nothing">订单里空空如也，赶紧去浏览添加吧</div>
                `
            }else{
                document.getElementById("myMessage").innerHTML = `<div class="order">已付款</div>`
                for(let i = 0;i<list.length;i++){
                    document.getElementById("myMessage").innerHTML += `
                    <div class="orderMessage">
                        <div class="shop"><a href="">${list[i].shopName}></a></div>
                        <div class="state">已付款</div>
                        <div class="goodPhoto"><a href=""><img src="/static/home/img/4.png"></a></div>
                        <div class="goodMessage">
                            <p class="title"><a href="">${list[i].goodsName}</a></p>
                            <p class="size"><a href=""></a></p>
                        </div>
                        <div class="goodPrice">
                            <p class="title">￥${list[i].goodsPrice}</p>
                            <p class="size">×2</p>
                        </div>
                        <div class="price">
                            <p>总价￥${2*list[i].goodsPrice}，优惠￥${2*list[i].goodsPrice}，实付￥0</p>
                        </div>
                        <div class="operate">
                            <button onclick="receiveGood()">确认收货</button>
                            <button onclick="searchExpressInform(${expressId})">状态查询</button>
                            <button onclick="refuse(${expressId},${orderId},${userId},)">拒收订单</button>
                        </div>
                    </div>
                    `
                }
            }
        }).catch(function (error){
                console.log(error) 
                document.getElementsByClassName("mainDiv")[0].innerHTML = `
                <div class="myMessage" id="myMessage">
                </div>
                `
                document.getElementById("myMessage").innerHTML = ``
                document.getElementById("myMessage").innerHTML += `
                <div class="nothing">订单里空空如也，赶紧去浏览添加吧</div>
                `
        })
    }
    (function(){queryRefuseExpress(1)})();
    // (function(){queryAll()})();
}
function deleteOrderHave(orderId,pageNo){
    console.log(pageNo)
    axios.put('/home/order/order/deleteOrderHave',{
        params:{
            orderId:orderId
        }
    }).then(function(data){
        var list = data.data.data.data
        if (data.data.data.code == 20000) {
            document.getElementById("floatContent").innerHTML = "<div>" + "此条订单已删除" + "</div>" + "<button onclick='closethis()' class='newbtn'>" + "确定" + "</button>";
            document.getElementById("floatContent").classList.remove("hide");
            queryAllOrderHave(pageNo)
        }else{
            document.getElementById("floatContent").innerHTML = "<div>" + "订单删除失败" + "</div>" + "<button onclick='closethis()' class='newbtn'>" + "确定" + "</button>";
            document.getElementById("floatContent").classList.remove("hide");
        }
    }).catch(function(error){
        console.log(error)
    })
}

//----------------------------------------------------------------------------------------------------//
//----------------------------------------------------------------------------------------------------//
//----------------------------------------------------------------------------------------------------//
function receiveGood(orderId,pageNo){
    axios.put('/home/order/express/userSureOrNoSent',{
        params:{
            orderId:orderId
        }
    }).then(function(data){
        var list = data.data.data.data
        if (data.data.data.code == 20000) {
            queryAllOrderHave(pageNo)
        }else{
            document.getElementById("floatContent").innerHTML = `<div>${data.data.data.data}</div><button onclick='closethis()' class='newbtn'>确定</button>`;
            document.getElementById("floatContent").classList.remove("hide");
        }
    }).catch(function(error){
        console.log(error)
    })
}
function refuse(expressId,orderId,userId){
    document.getElementById("floatContent").innerHTML = `
        <div>请输入拒收订单理由</div>
        <input type="text" class="reason" name="reason" id="reason" placeholder="拒收原因">
        <button onclick='closethis();refundornot(${expressId},${orderId});refuseExpress(${expressId},${orderId},${userId})' class='half'>确定</button>
        <button onclick='closethis()' class='half'>取消</button>
        `
    document.getElementById("floatContent").classList.remove("hide");
}
function refuseExpress(expressId,orderId,userId){
    var  reason = document.getElementById("reason").innerHTML
    axios.post('/home/expressmessage/refuseExpress/addRefuseExpress',{
        params:{
            expressId:expressId,
            orderId:orderId,
            reason:reason,
            userId:userId
        }
    }).then(function (data) {
        if (data.data.data.code == 20000) {
            document.getElementById("floatContent").innerHTML = "<div>" + "此条订单已拒收" + "</div>" + "<button onclick='closethis()' class='newbtn'>" + "确定" + "</button>";
            document.getElementById("floatContent").classList.remove("hide");
        }else{
            document.getElementById("floatContent").innerHTML = `<div>很抱歉</div><div>${data.data.data.data}</div><button onclick='closethis()' class='newbtn'>确定</button>`;
            document.getElementById("floatContent").classList.remove("hide");
        }
    }).catch(function (error) {
        console.log(error)
        document.getElementById("floatContent").innerHTML = "<div>" + "出错了，请检查网络连接" + "</div>" + "<button onclick='closethis()' class='newbtn'>" + "确定" + "</button>";
        document.getElementById("floatContent").classList.remove("hide");
    })
}
function refundornot(expressId,orderId){
    document.getElementById("floatContent").innerHTML = `
        <div>您的拒收请求已提交，您选择：</div>
        <input name="refund" type="radio" onchange="refund(${expressId},${orderId});closethis()">退款
        <input name="norefund" type="radio" onchange="closethis()">不退款
        `
    document.getElementById("floatContent").classList.remove("hide");
}
function refund(expressId,orderId){
    document.getElementById("floatContent").innerHTML = `
        <div>请输入退款理由</div>
        <input type="text" class="reason" name="reason" id="reason" placeholder="退款原因">
        <button onclick='closethis();refundExpress(${expressId},${orderId})' class='half'>确定</button>
        <button onclick='closethis()' class='half'>取消</button>
        `
    document.getElementById("floatContent").classList.remove("hide");
}
function refundExpress(expressId,orderId){
    var  reason = document.getElementById("reason").innerHTML
    axios.post('/home/expressmessage/refund/addRefund',{
        params:{
            expressId:expressId,
            orderId:orderId,
            reason:reason,
        }
    }).then(function (data) {
        if (data.data.data.code == 20000) {
            document.getElementById("floatContent").innerHTML = "<div>" + "退款申请已提交，正在审核中" + "</div>" + "<button onclick='closethis()' class='newbtn'>" + "确定" + "</button>";
            document.getElementById("floatContent").classList.remove("hide");
        }else{
            document.getElementById("floatContent").innerHTML = `<div>很抱歉</div><div>${data.data.data.data}</div><button onclick='closethis()' class='newbtn'>确定</button>`;
            document.getElementById("floatContent").classList.remove("hide");
        }
    }).catch(function (error) {
        console.log(error)
        document.getElementById("floatContent").innerHTML = "<div>" + "出错了，请检查网络连接" + "</div>" + "<button onclick='closethis()' class='newbtn'>" + "确定" + "</button>";
        document.getElementById("floatContent").classList.remove("hide");
    })
}
function searchExpressInform(expressId){
    document.querySelector(".mainDiv").innerHTML=`
    <div class="myMessage" id="myMessage"></div>
    `
    axios.get('/home/expressmessage/express/selectInformationInExpress',{
        params:{
            expressId:expressId
        }
    }).then(function(data){
        var list = data.data.data.data
        console.log(list)
        if (data.data.data.code == 20000) {
            document.getElementById("myMessage").innerHTML = `
                <div class="formbox">
                    <div class="addressForm">
                        <div class="addressee">${list.addressee},</div>
                        <div class="addresseeTele">${list.addresseeTele}</div>
                        <div class="myaddress">${list.address}</div>
                    </div>
                </div>
                <div class="orderMessage">
                    <div class="shop">${list.shopName}></div>
                    <div class="state">卖家已发货</div>
                    <div class="goodPhoto"><img src="${list.cover}"></div>
                    <div class="goodMessage">
                        <p class="title">${list.goodsName}</p>
                        <p class="size">${list.size}</p>
                    </div>
                    <div class="goodPrice">
                        <p class="title">￥${list.goodsPrice}</p>
                        <p class="size">×${list.count}</p>
                    </div>
                    <div class="price">
                        <p>总价￥${list.goodsPrice*list.count}，优惠${list.goodsPrice*list.count}，实付￥0</p>
                    </div>
                    <div class="operate">
                        <div class="time">
                            下单时间：${list.deliverGoodsTime}
                        </div>
                    </div>
                </div>
                <div class="orderSituation">
                    <div class="order">订单信息</div>
                    <div class="situation">快递编号：${list.expressNo}</div>
                    <div class="expressBrand">尊贵的零元购客户，您的快递正在由${list.expressBrandId}小哥护送，不日便将抵达目的地</div>
                </div>
            `
        }else{
            document.getElementById("floatContent").innerHTML = `<div>很抱歉</div><div>${data.data.data.data}</div><button onclick='closethis()' class='newbtn'>确定</button>`;
            document.getElementById("floatContent").classList.remove("hide");
        }
    }).catch(function(error){
        console.log(error)
        document.getElementById("floatContent").innerHTML = "<div>" + "出错了，请检查网络连接" + "</div>" + "<button onclick='closethis()' class='newbtn'>" + "确定" + "</button>";
        document.getElementById("floatContent").classList.remove("hide");
    })
}
function closethis(){
    document.getElementById("floatContent").classList.add("hide");
}